package tech.lq0.dreamaticvoyage.item.fukamizutech.material;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.Tags;
import org.jetbrains.annotations.Nullable;
import tech.lq0.dreamaticvoyage.client.tooltip.AcidCannoliComponent;
import tech.lq0.dreamaticvoyage.tools.RarityTool;

import java.util.List;
import java.util.Optional;

public class AcidCannoli extends Item {

    public static final int MAX_COUNT = 32;

    public AcidCannoli() {
        super(new Properties().stacksTo(1).fireResistant().rarity(RarityTool.FUKAMIZU_TIER2));
    }

    @Override
    public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents, TooltipFlag pIsAdvanced) {
        pTooltipComponents.add(Component.translatable("des.dreamaticvoyage.acid_cannoli").withStyle(ChatFormatting.GRAY));
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) {
        ItemStack stack = pPlayer.getItemInHand(pUsedHand);
        ItemStack material = pPlayer.getItemInHand(pUsedHand == InteractionHand.OFF_HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND);
        if (material.is(Tags.Items.RAW_MATERIALS)) {
            if (stack.getTag() == null || !stack.getTag().contains("Material")) {
                stack.getOrCreateTag().put("Material", material.serializeNBT());
                stack.getOrCreateTag().putInt("MaterialCount", 1);
                material.shrink(1);
                pLevel.playSound(null, pPlayer.getOnPos(), SoundEvents.GENERIC_BURN, SoundSource.BLOCKS, 0.4F, 2.0F + pLevel.random.nextFloat() * 0.4F);
                return InteractionResultHolder.success(stack);
            } else {
                var tag = stack.getTag().getCompound("Material");
                var mt = ItemStack.of(tag);
                if (mt.getItem() == material.getItem()) {
                    if (stack.getTag().getInt("MaterialCount") < MAX_COUNT) {
                        stack.getTag().putInt("MaterialCount", stack.getTag().getInt("MaterialCount") + 1);
                        material.shrink(1);
                        pLevel.playSound(null, pPlayer.getOnPos(), SoundEvents.GENERIC_BURN, SoundSource.BLOCKS, 0.4F, 2.0F + pLevel.random.nextFloat() * 0.4F);
                        return InteractionResultHolder.success(stack);
                    }
                }
            }
        }

        return InteractionResultHolder.fail(stack);
    }

    @Override
    public boolean isFoil(ItemStack pStack) {
        return pStack.getTag() != null && pStack.getTag().getInt("MaterialCount") >= MAX_COUNT;
    }

    @OnlyIn(Dist.CLIENT)
    @Override
    public Optional<TooltipComponent> getTooltipImage(ItemStack pStack) {
        if (pStack.hasTag()) {
            return Optional.of(new AcidCannoliComponent(pStack));
        }
        return Optional.empty();
    }
}
